<?php
		
	/* SVN FILE: $Id: select.php 8 2011-03-13 08:32:38Z michele.andreoletti@gmail.com $ */
	
	/**
	* Project Name : arcadia
	*
	* @author $Author: michele.andreoletti@gmail.com $
	* @version $Revision: 8 $
	* @lastrevision $Date: 2011-03-13 08:32:38 +0000 (Sun, 13 Mar 2011) $
	* @filesource $URL: http://arcadia.googlecode.com/svn/trunk/budget/select.php $
	*/

	require_once 'include/functions.php';
	require_once 'include/const.inc.php';
	require_once '../include/auth.inc.php';
	require_once '../include/conn.inc.php';
	
	$sOutput = '';
	$iMaxchar = 65; // massima lunghezza di una stringa di un campo <option>
		
	if (!isset($sWhere) || trim($sWhere)=="") { $sWhere = " 1 "; }

	fnGetValueFromGET(array('sSource','iDipartimento','iUnita', 'iShowAllResources', 'sViewItemLink', 'sTable'));
	
	if (!isset($sViewItemLink) || empty($sViewItemLink)) {
		fnEvaluateTablesAccessLvl($aTABLE_SECURITY_CHECK, false);
		if(!isset($sTable) || empty($sTable) || !isset($aTABLE[$sTable])) {
			$sTable = fnTableDefault($sTABLE_DEFAULT, $aTABLE);
		}
		fnTableConst($sTable);
	}
	
	fnViewConst($sViewItemLink);
	
	$sScript = fnCurrentScript();	

	// $sWhere
	if (trim($sTableSelectWhere) != '1') { $sWhere .= " AND ".$sTableSelectWhere; }
	// l'array $aWhere è stato inizializzata dalla funzione fnEvaluateTablesAccessLvl()
	if (isset($aWhere[$sTableSelectFrom]['sTableSelectWhere'])) { $sWhere .= $aWhere[$sTableSelectFrom]['sTableSelectWhere']; }
	
	if ($sSource == 'iDipartimento') {
	
		if (empty($iDipartimento)) {
			
			if (isset($iShowAllResources) && $iShowAllResources == 1) {
				
				// utilizzata in view.php (visualizzo tutte le risorse disponibili per poter creare un nuovo record in tabella) //
				
				$sQuery = "
					SELECT `us`.`id` AS `iUnita`, `us`.`sDescription` AS `sUnita`
					FROM `resources` `us`
					WHERE `sType` = 'US' AND `iShow` = '1'
					ORDER BY `us`.`sDescription` ASC";
				
			} else { 
			
				// utilizzata in index.php (visualizzo solo le risorse associate ai record della tabella) //
				
				$sQuery = "				
					SELECT `iUnita`, `sUnita`
					FROM `$sTableSelectFrom`
					WHERE $sWhere
					GROUP BY `sUnita` HAVING `sUnita` != '' ORDER BY `sUnita`";
					
			}
			
		} else if (!empty($iDipartimento)) { 
		
			if (isset($iShowAllResources) && $iShowAllResources == 1) {
			
				// utilizzata in view.php (visualizzo tutte le risorse disponibili per poter creare un nuovo record in tabella) //				
				
				$sQuery = "
					SELECT `us`.`id` AS `iUnita`, `us`.`sDescription` AS `sUnita`
					FROM `resources` `us`
					LEFT OUTER JOIN `properties` `iParent` ON `us`.`id` = `iParent`.`iTable` AND `iParent`.`sSelectFrom` = 'resources' AND `iParent`.`sField` = 'iParent' 			
					WHERE `sType` = 'US' AND `iShow` = '1' AND `iParent`.`sValue` = '$iDipartimento'
					ORDER BY `us`.`sDescription` ASC";
				
			} else {
			
				// utilizzata in index.php (visualizzo solo le risorse associate ai record della tabella) //			
				
				$sQuery = "				
					SELECT `iUnita`, `sUnita`
					FROM `$sTableSelectFrom`
					WHERE $sWhere AND `iDipartimento` = '$iDipartimento' ".
					(isset($iUnita) && !empty($iUnita) ? " OR `iUnita` = '$iUnita' " : '').
					" GROUP BY `sUnita` HAVING `sUnita` != '' ORDER BY `sUnita`";
					
			}

		}
		
		$rResult = custom_mysql_query($sQuery);
		
		if (mysql_num_rows($rResult) > 0) {
		
			$sOutput .= "[ ";
			$sOutput .= "{ \"iOptionValue\": \"\", \"sOptionDisplay\": \"Seleziona unità...\" }, ";
			while ($aRow = mysql_fetch_array($rResult)) {
				$sOutput .= "{ \"iOptionValue\": \"".$aRow['iUnita']."\", \"sOptionDisplay\": \"".
					(strlen($aRow['sUnita'])>$iMaxchar ? substr($aRow['sUnita'], 0, $iMaxchar-4)." (...)" : $aRow['sUnita'])."\" }, ";
			}
			$sOutput = substr($sOutput, 0, -2)." ]";
			
		} else {
		
			$sOutput .= "[ { \"iOptionValue\": \"\", \"sOptionDisplay\": \"Nessun dato trovato\" } ]";
		
		} 

		
	} else if ($sSource == 'iUnita') {
	
		if(empty($iUnita)) {
				
			$sOutput = "[ { \"iOptionValue\": \"$iDipartimento\" } ]";
		
		} else if(!empty($iUnita)) { 
			
			$sQuery = "
				SELECT `dip`.`id` AS `iDipartimento`, `dip`.`sDescription` AS `sDipartimento`
				FROM `resources` `us`
				LEFT OUTER JOIN `properties` `iParent` ON `us`.`id` = `iParent`.`iTable` AND `iParent`.`sSelectFrom` = 'resources' AND `iParent`.`sField` = 'iParent' 			
				LEFT OUTER JOIN `resources` `dip` ON `iParent`.`sValue` = `dip`.`id`
				WHERE `us`.`id` =' $iUnita' AND `us`.`sType` = 'US' AND `dip`.`sType` = 'DIP'
				GROUP BY `dip`.`id`";
			
			$rResult = custom_mysql_query($sQuery);	
			
			if (mysql_num_rows($rResult) == 1) {
			
				$aRow = mysql_fetch_array($rResult);
				$sOutput = "[ { \"iOptionValue\": \"".$aRow['iDipartimento']."\" } ]";
			
			} else {
				
				$sOutput = "[ { \"iOptionValue\": \"\" } ]";
			
			}
			
		}
	
	}
	
	echo $sOutput;
	die();
?>

